package org.demo.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.demo.common.Result;
import org.demo.entity.*;
import org.demo.mapper.AppointmentsMapper;
import org.demo.mapper.CommunityMapper;
import org.demo.mapper.HousesMapper;
import org.demo.mapper.UsersMapper;
import org.demo.service.IAppointmentsService;
import org.demo.service.INoticeService;
import org.demo.service.IOrderIdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;


@Service
public class AppointmentsServiceImpl extends ServiceImpl<AppointmentsMapper, Appointments> implements IAppointmentsService {
    @Autowired
    private AppointmentsMapper appointmentsMapper;
    @Autowired
    private UsersMapper usersMapper;
    @Autowired
    private HousesMapper housesMapper;
    @Autowired
    private CommunityMapper communityMapper;
    @Autowired
    private IOrderIdService orderIdService;
    @Autowired
    private INoticeService noticeService;

    @Override
    public void add(Appointments appointments) {
        String IntId=appointments.getIntermediaryId();
        String CusId=appointments.getCustomerId();
        Houses houses =housesMapper.selectById(appointments.getHouseId());
        String id = orderIdService.getId("COMM");
        appointments.setId(id);
        appointments.setAppointmentTime(appointments.getAppointmentTime());
        appointments.setStatus("0");
        appointments.setType(houses.getType());
        String IntTel = usersMapper.selectById(IntId).getPhone();
        String CusTel = usersMapper.selectById(CusId).getPhone();
        appointments.setCusTel(CusTel);
        appointments.setIntTel(IntTel);
        save(appointments);


        String content="您收到了新的看房预约";
        noticeService.addNotice(appointments.getCustomerId(),appointments.getIntermediaryId(),appointments.getHouseId(),content,"0","0","0");


    }

    @Override
    public List<Appointments> list(Appointments appointments) {
        List<Appointments> appointments1=lambdaQuery().eq(Appointments::getId,appointments.getId()).list();


        return appointments1;
    }

    @Override
    public void updateAppointmentStatusOk(Appointments appointmentId) {
        Appointments appointments = appointmentsMapper.selectById(appointmentId.getId());
        if (appointments.getStatus().equals("0")){
            appointments.setStatus("1");

        }
        appointmentsMapper.updateById(appointments);

        String content="中介已确定您的看房预约";
        noticeService.addNotice(appointments.getIntermediaryId(),appointments.getCustomerId(),appointments.getHouseId(),content,"0","0","0");


    }

    @Override
    public void delete(String id) {
        Appointments appointments = appointmentsMapper.selectById(id);
        String content="您的看房预约已取消";
        noticeService.addNotice(appointments.getIntermediaryId(),appointments.getCustomerId(),appointments.getHouseId(),content,"0","0","0");


        appointmentsMapper.deleteById(id);

    }

    @Override
    public PageBean<AppointmentsDto> Pagelist(Integer pageNum, Integer pageSize, AppointmentsDto appointmentsD) {
        // 创建 pagebean 对象
        PageBean<AppointmentsDto> pb = new PageBean<>();
        // 开启分页查询
        Page<Appointments> page = PageHelper.startPage(pageNum, pageSize);

        List<Appointments> appointment=appointmentsMapper.selectList(appointmentsD);
        List<AppointmentsDto> appointmentsList = new ArrayList<>();

        for (int i = 0; i < appointment.size(); i++) {
            Houses houses = housesMapper.selectById(appointment.get(i).getHouseId());
            Community community = communityMapper.selectById(houses.getCommunityId());
            Users cus = usersMapper.selectById(appointment.get(i).getCustomerId());
            Users intN = usersMapper.selectById(appointment.get(i).getIntermediaryId());
            AppointmentsDto appointmentsDto = BeanUtil.copyProperties(appointment.get(i), AppointmentsDto.class);
            appointmentsDto.setCustomerName(cus.getNickname());
            appointmentsDto.setIntermediaryName(intN.getNickname());
            appointmentsDto.setUserCusPic(cus.getUserPic());
            appointmentsDto.setUserIntPic(intN.getUserPic());
            appointmentsDto.setAddress(houses.getAddress());
            appointmentsDto.setType(houses.getType());
            appointmentsDto.setCommunityId(community.getId());
            appointmentsDto.setCommunityName(community.getName());

            appointmentsList.add(appointmentsDto);
        }
        pb.setTotal(page.getTotal());
        pb.setItems(appointmentsList);

        return pb;
    }
}
